package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.VO.ArticleAndReportVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.demo.service.IArticleAndReportService;
import com.example.demo.entity.ArticleAndReport;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 举报文章表 前端控制器
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-12
 */
@Api(tags = "举报文章表")
@RestController
@RequestMapping("/article-and-report")
public class ArticleAndReportController {

    @Resource
    private IArticleAndReportService articleAndReportService;

    @Resource
    private IUserService userService;

    @Resource
    private IArticleService articleService;

    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "新增举报")
    @PostMapping("/add")
    public Result<String> add(@RequestBody ArticleAndReportVO articleAndReportVO) {
        ArticleAndReport articleAndReport = new ArticleAndReport();
        BeanUtil.copyProperties(articleAndReportVO,articleAndReport);
        //判断用户id是否存在
        if(userService.findNicknameByUserId(articleAndReport.getUserId()) == null){
            return Result.error().msg("举报失败,用户id不存在");
        }
        //判断文章是否存在
        if(articleService.findArticleById(articleAndReport.getArticleId()) == null){
            return Result.error().msg("举报失败,文章id不存在");
        }
        if(!articleAndReportService.addArticleAndReport(articleAndReport)){
            return Result.error().msg("举报失败");
        }
        return Result.success().msg("举报成功");
    }
}